﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class PatientScheduler : System.Web.UI.Page
{
    private DateTime[] SpecialDays = new DateTime[] { new DateTime(2013, 3, 14), new DateTime(2013,3,13) };
    protected void Page_Init(object sender, EventArgs e)
    {


    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //PatientsScheduler.VisibleRangeEnd 
    }

    protected void PatientsScheduler_ResourceHeaderCreated(object sender, ResourceHeaderCreatedEventArgs e)
    {
        Panel ResourceImageWrapper = e.Container.FindControl("ResourceImageWrapper") as Panel;
        ResourceImageWrapper.CssClass = "Resource" + e.Container.Resource.Key.ToString();

        Image img = e.Container.FindControl("DoctorImage") as Image;
        img.ImageUrl = "App_Themes/Default/Images/carvalho.png";
        //"Img/" + e.Container.Resource.Text.Split(' ')[1].ToLower() + ".png";
    }

    protected void GroupingDirectionComboBox_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        UpdateScheduler();
    }

    private void UpdateScheduler()
    {
        PatientsScheduler.GroupingDirection =
            (GroupingDirection)Enum.Parse(typeof(GroupingDirection), GroupingDirectionComboBox.SelectedValue);

        PatientsScheduler.Rebind();
    }
    protected void PatientsScheduler_FormCreating(object sender, SchedulerFormCreatingEventArgs e)
    {
        if (e.Mode == SchedulerFormMode.Insert || e.Mode == SchedulerFormMode.AdvancedInsert)
        {
            e.Appointment.Resources.Add(PatientsScheduler.Resources.GetResourceByType("AppointmentType"));
            e.Appointment.Resources.Add(PatientsScheduler.Resources.GetResourceByType("Room"));
            e.Appointment.Resources.Add(PatientsScheduler.Resources.GetResourceByType("Doctor"));
        }
    }
    protected void PatientsScheduler_DataBound(object sender, EventArgs e)
    {
        PatientsScheduler.ResourceTypes.FindByName("AppointmentType").AllowMultipleValues = false;
        PatientsScheduler.ResourceTypes.FindByName("Doctor").AllowMultipleValues = false;
        PatientsScheduler.ResourceTypes.FindByName("Room").AllowMultipleValues = false;
    }
    protected void PatientsScheduler_AppointmentCreated(object sender, AppointmentCreatedEventArgs e)
    {
        if (e.Appointment.RecurrenceState == RecurrenceState.Master || e.Appointment.RecurrenceState == RecurrenceState.Occurrence)
        {
            Panel recurrenceStateDiv = new Panel();
            recurrenceStateDiv.CssClass = "rsAptRecurrence";
            e.Container.Controls.AddAt(0, recurrenceStateDiv);
        }
        if (e.Appointment.RecurrenceState == RecurrenceState.Exception)
        {
            Panel recurrenceStateDiv = new Panel();
            recurrenceStateDiv.CssClass = "rsAptRecurrenceException";
            e.Container.Controls.AddAt(0, recurrenceStateDiv);
        }
    }

    protected void PatientsScheduler_AppointmentDataBound(object sender, SchedulerEventArgs e)
    {
        e.Appointment.ToolTip = string.Format("{0}:\n {1}\n{2}" ,e.Appointment.Subject ,e.Appointment.Resources[0].Text,e.Appointment.Resources[1].Text);
        foreach (DateTime dt in SpecialDays)
        {
            if ((DateTime.Compare(e.Appointment.Start.Date, dt) == 0) || (DateTime.Compare(e.Appointment.End.Date, dt) == 0))
            {
                e.Appointment.AllowDelete = false;
                e.Appointment.AllowEdit = false;
            }
        }
    }
    protected void PatientsScheduler_TimeSlotCreated(object sender, TimeSlotCreatedEventArgs e)
    {
        //RadScheduler scheduler = (RadScheduler)sender;
        //if (scheduler.SelectedView == SchedulerViewType.DayView)
        //{
        //    if (e.TimeSlot.Start.Hour >= 9 && e.TimeSlot.Start.Hour < 13)
        //    {
        //        e.TimeSlot.CssClass = "Enabled";
        //    }
        //    else
        //    {
        //        e.TimeSlot.CssClass = "Disabled";
        //    }
        //}
    }
    protected void PatientsScheduler_FormCreated(object sender, SchedulerFormCreatedEventArgs e)
    {
        RadScheduler scheduler = (RadScheduler)sender;

        if (e.Container.Mode == SchedulerFormMode.Insert ||
            e.Container.Mode == SchedulerFormMode.AdvancedInsert)
        {
            //TextBox subjectBox = (TextBox)e.Container.FindControl("FirstNameTextBox");
            //subjectBox.Text = "fghfjhgh";

            //RadDateInput startInput = (RadDateInput)e.Container.FindControl("StartInput");
            //startInput.DateFormat = scheduler.AdvancedForm.DateFormat + " " + scheduler.AdvancedForm.TimeFormat;
            //startInput.SelectedDate = PatientsScheduler.DisplayToUtc(e.Appointment.Start);

            //RadDateInput endInput = (RadDateInput)e.Container.FindControl("EndInput");
            //endInput.DateFormat = scheduler.AdvancedForm.DateFormat + " " + scheduler.AdvancedForm.TimeFormat;
            //endInput.SelectedDate = PatientsScheduler.DisplayToUtc(e.Appointment.End);

            RadComboBox appointmentTypeComboBox = (RadComboBox)e.Container.FindControl("AppointmentTypeComboBox");
            appointmentTypeComboBox.SelectedIndex = 1;
        }
    }
    protected void InsertButton_Click(object sender, EventArgs e)
    {

    }
}
/*
 foreach (Appointment a in RadScheduler1.Appointments)  
        {  
            //Occurrences of a recurrence master are not generated untill they      
            //are within RadScheduler's visible range. This mandates the occurrences     
            //information to be extracted from the Master's recurrence rule as follows:     
            if (a.RecurrenceState == RecurrenceState.Master)  
            {  
                RecurrenceRule parsedRule;  
                RecurrenceRule.TryParse(a.RecurrenceRule.ToString(), out parsedRule);  
 
                //If a recurring appointment does not have specified end time it will have     
                //endless occurrences. In this case you can explicitly put a restriction:     
                //parsedRule.SetEffectiveRange(DateTime.Now.AddYears(-1), DateTime.Now.AddYears(1));     
                foreach (DateTime occurrence in parsedRule.Occurrences)  
                {  
                    table1.Rows.Add(new string[] { a.Subject.ToString(), occurrence.ToString(), occurrence.Add(a.Duration).ToString() });  
                }  
            }  
            else //Get the rest of the appointments     
 
                //If the SelectedDate of RadScheduler is set so that the visible range encompasses occurences of      
                //a recurring appointement, these occurences will be generated now so they will be added once again     
                //to the DataTable. To prevent this, we use the following check:     
                if (a.RecurrenceState != RecurrenceState.Occurrence)  
                {  
                    table1.Rows.Add(new string[] { a.Subject.ToString(), a.Start.ToString(), a.End.ToString() });  
                }  
        }  
 */



/*
 * http://www.telerik.com/support/kb/aspnet-ajax/scheduler/setting-special-days-or-time-slots-in-radscheduler.aspx
 * 
protected void RadScheduler1_TimeSlotCreated(object sender, TimeSlotCreatedEventArgs e)
{
 RadScheduler scheduler = (RadScheduler)sender;
 if (scheduler.SelectedView==SchedulerViewType.DayView)
 {
   if (e.TimeSlot.Start.Hour >= 9 && e.TimeSlot.Start.Hour < 13)
   {
    e.TimeSlot.CssClass = "Enabled";
   }
   else
   {
      e.TimeSlot.CssClass = "Disabled";
   }
 }
}
 * .Disabled
{
  background-color:Grey !important;
}
.Enabled
{
  background-color:Red !important;
}

*/

/*
protected void RadScheduler1_AppointmentInsert(object sender, AppointmentInsertEventArgs e)
{
    if (!IsValidAppointment(e.Appointment))
        e.Cancel = true;
}
 
  private bool IsValidAppointment(Appointment apt)
{
    bool isValid = true;
    if (apt.RecurrenceState == RecurrenceState.Master)
    {
        RecurrenceRule parsedRule;
        RecurrenceRule.TryParse(apt.RecurrenceRule.ToString(), out parsedRule);
        DateTime[] twoOccurrences = new DateTime[2];
 
        var enumerator = parsedRule.Occurrences.GetEnumerator();
 
        int i = 0;
        for (; i < 2; i++)
            if (enumerator.MoveNext())
                twoOccurrences[i] = enumerator.Current;
            else
                break;
 
        if (i == 2)
            isValid = !(apt.Duration > (twoOccurrences[1] - twoOccurrences[0]));
    }
 
    return isValid;
}

  
*/

/*//Set your special days here:
 *  <style type="text/css">  
        .Disabled  
        {  
            background: silver !important;  
            cursor: not-allowed;  
        }  
        .Disabled.rsAptCreate  
        {  
            background: silver !important;  
        }  
    </style> 
 * 
 * protected void RadScheduler1_AppointmentDataBound(object sender, SchedulerEventArgs e)
    {
        foreach (DateTime dt in SpecialDays)
        {
            if ((DateTime.Compare(e.Appointment.Start.Date, dt) == 0) || (DateTime.Compare(e.Appointment.End.Date, dt) == 0))
            {
                e.Appointment.AllowDelete = false;
                e.Appointment.AllowEdit = false;
            }
        }
    }

 * 
    private readonly DateTime[] SpecialDays = new DateTime[] { new DateTime(2008, 6, 26), new DateTime(2008, 6, 27) };
  
    protected void RadScheduler1_TimeSlotCreated(object sender, Telerik.Web.UI.TimeSlotCreatedEventArgs e)
    {
        foreach (DateTime dt in SpecialDays)
        {
            if (DateTime.Compare(e.TimeSlot.Start.Date, dt) == 0)
            {
                //Set the CssClass property to visually distinguish your special days.
                e.TimeSlot.CssClass = "Disabled";
            }
        }
    }
  
    protected void RadScheduler1_AppointmentUpdate(object sender, Telerik.Web.UI.AppointmentUpdateEventArgs e)
    {
        foreach (DateTime dt in SpecialDays)
        {
            if ((DateTime.Compare(e.ModifiedAppointment.Start.Date, dt) == 0) || (DateTime.Compare(e.ModifiedAppointment.End.Date, dt) == 0))
            {
                e.Cancel = true;
                ScriptManager.RegisterStartupScript(Page, GetType(), "alert", "alert('The duration of appointment contains Special Days!');", true);
            }
        }
    }

 
 */